<!-- eslint-disable unused-imports/no-unused-vars -->
<script lang="ts" setup>
import { Edit, Star } from "@element-plus/icons-vue"
import { ElButton, ElMessageBox } from "element-plus"
import { useRouter } from "vue-router"
import { useResumeStore } from "@/pinia/stores/resume"

// 使用 store
const resumeStore = useResumeStore()
const router = useRouter()

// 处理编辑简历
function handleEditResume() {
  if (resumeStore.currentDetail?.resumeId) {
    router.push({
      path: `/internship/resume/edit/${resumeStore.currentDetail.resumeId}`
    })
  }
}

// 处理设置默认简历
async function handleSetDefault() {
  if (!resumeStore.currentDetail || resumeStore.currentDetail.isDefault === 1) {
    return
  }

  try {
    await ElMessageBox.confirm(
      `确定要将简历「${resumeStore.currentDetail.resumeName || "未命名简历"}」设为默认简历吗？`,
      "设置默认简历",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
        center: true, // 确保弹窗内容居中
        showClose: true, // 显示关闭按钮
        customClass: "default-resume-dialog" // 添加自定义类以便进一步样式优化
      }
    )

    // 更新简历的isDefault字段
    await resumeStore.updateResume(resumeStore.currentDetail.resumeId, {
      userId: resumeStore.currentDetail.userId,
      resumeName: resumeStore.currentDetail.resumeName,
      isDefault: 1,
      skills: resumeStore.currentDetail.skills || [],
      internExperiences: resumeStore.currentDetail.internExperiences || [],
      languages: resumeStore.currentDetail.languages || [],
      awards: resumeStore.currentDetail.awards || [],
      otherInfo: resumeStore.currentDetail.otherInfo || "",
      resumePdfUrl: resumeStore.currentDetail.resumePdfUrl || "",
      status: Number.parseInt(resumeStore.currentDetail.status) || 1
    })

    // 重新获取详情以更新状态
    await resumeStore.fetchResumeDetail(resumeStore.currentDetail.resumeId)
  } catch (error) {
    // 取消设置时不做处理
  }
}

// 格式化创建时间
function formatTime(timeStr?: string) {
  if (!timeStr) return ""
  return new Date(timeStr).toLocaleDateString("zh-CN", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit"
  })
}
</script>

<template>
  <div class="resume-detail scrollable-content">
    <!-- 加载状态 -->
    <div v-if="resumeStore.loading" class="loading-state">
      <p>加载中...</p>
    </div>

    <!-- 简历详情内容 -->
    <div v-else-if="resumeStore.currentDetail" class="detail-content">
      <!-- 详情头部 -->
      <div class="detail-header">
        <div class="title-section">
          <h2 class="position-title">
            {{ resumeStore.currentDetail.resumeName }}
          </h2>
          <div class="action-buttons-header">
            <ElButton
              v-if="resumeStore.currentDetail?.isDefault !== 1"
              size="large"
              @click="handleSetDefault"
              type="info"
            >
              <Star /> 设为默认
            </ElButton>
            <ElButton type="primary" size="large" @click="handleEditResume">
              <Edit /> 编辑简历
            </ElButton>
          </div>
        </div>

        <div class="resume-meta">
          <el-tag
            v-if="resumeStore.currentDetail.isDefault === 1"
            size="small"
            type="primary"
            class="default-badge"
          >
            默认简历
          </el-tag>
          <span class="meta-item" :class="resumeStore.currentDetail.status === '正常' ? 'status-active' : 'status-inactive'">
            状态：{{ resumeStore.currentDetail.status }}
          </span>
          <span class="meta-item">
            创建时间：{{ formatTime(resumeStore.currentDetail.createTime) }}
          </span>
          <span class="meta-item" v-if="resumeStore.currentDetail.updateTime">
            更新时间：{{ formatTime(resumeStore.currentDetail.updateTime) }}
          </span>
        </div>
      </div>

      <!-- 技能特长 -->
      <div v-if="resumeStore.currentDetail.skills && resumeStore.currentDetail.skills.length > 0" class="detail-section">
        <h3 class="section-title">
          技能特长
        </h3>
        <div class="section-content">
          <div class="skill-list">
            <el-tag
              v-for="(skill, index) in resumeStore.currentDetail.skills"
              :key="index"
              size="default"
              type="info"
              class="skill-tag-enhanced"
              effect="light"
            >
              <strong class="skill-name">{{ skill.skillName || '未命名技能' }}</strong>
              <span v-if="skill.proficiency" class="skill-level">
                - {{ skill.proficiency }}
              </span>
            </el-tag>
          </div>
        </div>
      </div>

      <!-- 实习经历 -->
      <div v-if="resumeStore.currentDetail.internExperiences && resumeStore.currentDetail.internExperiences.length > 0" class="detail-section">
        <h3 class="section-title">
          实习经历
        </h3>
        <div class="section-content">
          <div
            v-for="(exp, index) in resumeStore.currentDetail.internExperiences"
            :key="index"
            class="exp-item"
          >
            <div class="exp-header">
              <span class="company-name">{{ exp.company || '未知公司' }}</span>
              <span class="period">{{ exp.startDate }} - {{ exp.endDate || '至今' }}</span>
            </div>
            <div class="exp-position">
              {{ exp.position || '未知职位' }}
            </div>
            <div v-if="exp.content" class="exp-description">
              {{ exp.content }}
            </div>
          </div>
        </div>
      </div>

      <!-- 语言能力 -->
      <div v-if="resumeStore.currentDetail.languages && resumeStore.currentDetail.languages.length > 0" class="detail-section">
        <h3 class="section-title">
          语言能力
        </h3>
        <div class="section-content">
          <div
            v-for="(lang, index) in resumeStore.currentDetail.languages"
            :key="index"
            class="lang-item"
          >
            <span class="lang-name">{{ lang.language || '未知语言' }}</span>
            <span class="lang-level">{{ lang.level || '未知水平' }}</span>
            <span class="lang-score">{{ lang.score || '未知成绩' }}</span>
          </div>
        </div>
      </div>

      <!-- 获奖情况 -->
      <div v-if="resumeStore.currentDetail.awards && resumeStore.currentDetail.awards.length > 0" class="detail-section">
        <h3 class="section-title">
          获奖情况
        </h3>
        <div class="section-content">
          <div
            v-for="(award, index) in resumeStore.currentDetail.awards"
            :key="index"
            class="award-item"
          >
            <div class="award-header">
              <span class="award-name">{{ award.name || '未知奖项' }}</span>
              <span class="award-date">{{ award.getDate || '未知时间' }}</span>
            </div>
            <div v-if="award.issuer" class="award-description">
              {{ award.issuer || '未知机构' }}
            </div>
          </div>
        </div>
      </div>

      <!-- 其他信息 -->
      <div v-if="resumeStore.currentDetail.otherInfo" class="detail-section">
        <h3 class="section-title">
          其他信息
        </h3>
        <div class="section-content">
          <p class="other-info">
            {{ resumeStore.currentDetail.otherInfo }}
          </p>
        </div>
      </div>

      <!-- PDF预览 -->
      <div v-if="resumeStore.currentDetail.resumePdfUrl" class="detail-section">
        <h3 class="section-title">
          简历预览
        </h3>
        <div class="section-content">
          <el-link type="primary" :href="resumeStore.currentDetail.resumePdfUrl" target="_blank">
            查看PDF版本
          </el-link>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div v-else class="empty-state">
      <p>请选择一个简历查看详情</p>
    </div>
  </div>
</template>

<style scoped lang="scss">
.resume-detail {
  padding: 20px;
  height: 100%;
  overflow-y: auto;
  min-height: 400px;
  max-height: calc(100vh - 200px);
}

// 确保父容器也有适当的高度设置
:deep(.el-scrollbar__wrap) {
  overflow-y: auto;
}

.detail-header {
  margin-bottom: 24px;
}

.title-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f3f4f6;
}

.position-title {
  font-size: 22px;
  font-weight: 600;
  margin: 0;
  color: #1f2937;
  flex: 1;
  padding-right: 20px;
}

.action-buttons-header {
  display: flex;
  gap: 12px;
}

.resume-meta {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.default-badge {
  height: 24px;
  line-height: 24px;
}

.meta-item {
  font-size: 14px;
  color: #6b7280;
}

.meta-item.status-active {
  color: #10b981;
}

.meta-item.status-inactive {
  color: #ef4444;
}

.detail-section {
  margin-bottom: 24px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 12px 0;
  padding-bottom: 8px;
  border-bottom: 1px solid #e5e7eb;
}

.section-content {
  font-size: 14px;
  line-height: 1.6;
  color: #374151;
}

// 技能标签样式
.skill-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.skill-tag-enhanced {
  padding: 8px 12px;
  font-size: 14px;
  line-height: 1.5;
  border-radius: 6px;
  transition: all 0.3s ease;
  border: 1px solid #e0e0e0;
}

.skill-tag-enhanced:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.skill-name {
  color: #1f2937;
  font-weight: 600;
}

.skill-level {
  color: #6b7280;
  font-weight: 400;
  margin-left: 4px;
}

// 实习经历样式
.exp-item {
  margin-bottom: 16px;
  padding: 12px;
  background-color: #f9fafb;
  border-radius: 8px;
}

.exp-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
  font-weight: 500;
}

.company-name {
  color: #1f2937;
}

.period {
  color: #6b7280;
  font-size: 12px;
}

.exp-position {
  color: #6b7280;
  margin-bottom: 8px;
}

.exp-description {
  color: #4b5563;
  line-height: 1.6;
}

// 语言能力样式
.lang-item {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid #f3f4f6;
}

.lang-item:last-child {
  border-bottom: none;
}

.lang-name {
  font-weight: 500;
}

.lang-level {
  color: #6b7280;
}

// 获奖情况样式
.award-item {
  margin-bottom: 16px;
  padding: 12px;
  background-color: #f9fafb;
  border-radius: 8px;
}

.award-item:last-child {
  margin-bottom: 0;
}

.award-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
  font-weight: 500;
}

.award-name {
  color: #1f2937;
}

.award-date {
  color: #6b7280;
  font-size: 12px;
}

.award-description {
  color: #4b5563;
  line-height: 1.6;
}

// 其他信息样式
.other-info {
  line-height: 1.8;
  white-space: pre-wrap;
  word-break: break-word;
}

// 空状态样式
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 300px;
  color: #6b7280;
}

// 加载状态样式
.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 300px;
  color: #6b7280;
}

// 按钮样式
.el-button {
  font-weight: 500;

  :deep(.el-icon) {
    margin-right: 4px;
  }
}

// 增强滚动条样式
.scrollable-content::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.scrollable-content::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.scrollable-content::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
  transition: background 0.2s;
}

.scrollable-content::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

// 添加对Firefox的支持
.scrollable-content {
  scrollbar-width: thin;
  scrollbar-color: #c1c1c1 #f1f1f1;
}

// 自定义默认简历弹窗样式
:deep(.default-resume-dialog) {
  .el-message-box__header {
    padding: 20px 24px 16px;
  }

  .el-message-box__title {
    font-size: 16px;
    font-weight: 600;
    color: #1f2937;
  }

  .el-message-box__content {
    padding: 0 24px 24px;
    font-size: 14px;
    line-height: 1.5;
    color: #374151;
  }

  .el-message-box__footer {
    padding: 0 24px 20px;
    border-top: none;
  }
}
</style>
